package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param
     *
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders,HttpSession httpSession){
        log.info("订单数据：{}",orders);
        orderService.submit(orders, httpSession);
        return R.success("下单成功");
    }
    @GetMapping("/userPage")
    public R<Page> orderPage(int page,int pageSize,HttpSession httpSession){
        log.info("page={},pagesize={}",page,pageSize);
        //创建分页器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Long userId = (Long) httpSession.getAttribute("user");
        //创建条件筛选
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId );
        queryWrapper.orderByAsc(Orders::getOrderTime);
        //获取数据
        orderService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }
}